package com.msconfig.manager;

import android.support.annotation.AnimRes;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.annotation.StyleRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;


public class DefaultFragmentTransaction extends FragmentTransaction {

    final private FragmentManager fragmentManager;
    final private FragmentTransaction fragmentTransaction;

    public DefaultFragmentTransaction(FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
        this.fragmentTransaction = fragmentManager.beginTransaction();//获取事务实例
    }

    /**
     * 进栈操作</br>
     * 当事务允许堆栈操作时，操作事件进栈，当调用popBackStack()时，可回滚。</br>
     * 当事务不允许堆栈操作时，操作事件不进栈，事件不可回滚。
     */
    public void doAndAddToBackStack(){
        if(isAddToBackStackAllowed()){
            addToBackStack(null).commit();
        }else{
            commit();
        }
    }


    //要是切换的时候加载网络数据，使用replace会不断加载数据
    // 防止fragment加载多次数据，就是通过add保存到一个栈中
    public DefaultFragmentTransaction showSingleView(@IdRes int containerViewId, @NonNull Fragment fragment) {
        if (!fragment.isAdded()) {    //没有添加，先添加到栈中
            add(containerViewId, fragment);
        } else {
            show(fragment); // 已经添加过过了，直接显示
        }
        return this;
    }


    @Override
    public DefaultFragmentTransaction add(Fragment fragment, String tag) {
        fragmentTransaction.add(fragment,tag);
        return this;
    }

    @Override
    public DefaultFragmentTransaction add(@IdRes int containerViewId, Fragment fragment) {
        fragmentTransaction.add(containerViewId,fragment);
        return this;
    }

    @Override
    public DefaultFragmentTransaction add(@IdRes int containerViewId, Fragment fragment, @Nullable String tag) {
        fragmentTransaction.add(containerViewId,fragment,tag);
        return this;
    }

    @Override
    public DefaultFragmentTransaction replace(@IdRes int containerViewId, Fragment fragment) {
        fragmentTransaction.replace(containerViewId,fragment);
        return this;
    }

    @Override
    public DefaultFragmentTransaction replace(@IdRes int containerViewId, Fragment fragment, @Nullable String tag) {
        fragmentTransaction.replace(containerViewId,fragment,tag);
        return this;
    }

    @Override
    public DefaultFragmentTransaction remove(Fragment fragment) {
        fragmentTransaction.remove(fragment);
        return this;
    }

    @Override
    public DefaultFragmentTransaction hide(Fragment fragment) {
        if(fragment!=null)
            fragmentTransaction.hide(fragment);
        return this;
    }

    @Override
    public DefaultFragmentTransaction show(Fragment fragment) {
        fragmentTransaction.show(fragment);
        return this;
    }

    @Override
    public DefaultFragmentTransaction detach(Fragment fragment) {
        fragmentTransaction.detach(fragment);
        return this;
    }

    @Override
    public DefaultFragmentTransaction attach(Fragment fragment) {
        fragmentTransaction.attach(fragment);
        return this;
    }

    @NonNull
    @Override
    public FragmentTransaction setPrimaryNavigationFragment(@Nullable Fragment fragment) {
        return null;
    }

    @Override
    public boolean isEmpty() {
        return fragmentTransaction.isEmpty();
    }

    @Override
    public DefaultFragmentTransaction setCustomAnimations(@AnimRes int enter, @AnimRes int exit) {
        fragmentTransaction.setCustomAnimations(enter,exit);
        return this;
    }

    @Override
    public DefaultFragmentTransaction setCustomAnimations(@AnimRes int enter, @AnimRes int exit, @AnimRes int popEnter, @AnimRes int popExit) {
        fragmentTransaction.setCustomAnimations(enter,exit,popEnter,popExit);
        return this;
    }

    @Override
    public DefaultFragmentTransaction addSharedElement(View sharedElement, String name) {
        fragmentTransaction.addSharedElement(sharedElement,name);
        return this;
    }

    @Override
    public DefaultFragmentTransaction setTransition(int transit) {
        fragmentTransaction.setTransition(transit);
        return this;
    }

    @Override
    public DefaultFragmentTransaction setTransitionStyle(@StyleRes int styleRes) {
        fragmentTransaction.setTransitionStyle(styleRes);
        return this;
    }

    @Override
    public DefaultFragmentTransaction addToBackStack(@Nullable String name) {
        fragmentTransaction.addToBackStack(name);
        return this;
    }

    @Override
    public boolean isAddToBackStackAllowed() {
        return fragmentTransaction.isAddToBackStackAllowed();
    }

    @Override
    public DefaultFragmentTransaction disallowAddToBackStack() {
        fragmentTransaction.disallowAddToBackStack();
        return this;
    }

    @Override
    public DefaultFragmentTransaction setBreadCrumbTitle(@StringRes int res) {
        fragmentTransaction.setBreadCrumbTitle(res);
        return this;
    }

    @Override
    public DefaultFragmentTransaction setBreadCrumbTitle(CharSequence text) {
        fragmentTransaction.setBreadCrumbTitle(text);
        return this;
    }

    @Override
    public DefaultFragmentTransaction setBreadCrumbShortTitle(@StringRes int res) {
        fragmentTransaction.setBreadCrumbShortTitle(res);
        return this;
    }

    @Override
    public DefaultFragmentTransaction setBreadCrumbShortTitle(CharSequence text) {
        fragmentTransaction.setBreadCrumbShortTitle(text);
        return this;
    }

    @NonNull
    @Override
    public FragmentTransaction setReorderingAllowed(boolean reorderingAllowed) {
        return null;
    }

    @Override
    public FragmentTransaction setAllowOptimization(boolean allowOptimization) {
        return null;
    }

    @NonNull
    @Override
    public FragmentTransaction runOnCommit(@NonNull Runnable runnable) {
        return null;
    }

    @Override
    public int commit() {
        return fragmentTransaction.commit();
    }

    @Override
    public int commitAllowingStateLoss() {
        return fragmentTransaction.commitAllowingStateLoss();
    }

    @Deprecated
    public void commitNow() {

    }

    @Deprecated
    public void commitNowAllowingStateLoss() {

    }

}
